﻿using System;
using System.Collections.Generic;
using System.Text;
using PPI.Entities.Enumerator;
using PPI.Entities.Lookup;
using PPI.Entities.Remedy;
using PPI.Entities.Utility;

namespace PPI.Entities.Inspections
{
    public class EInspection :EBasePosition
    {
        #region private
        private Int32 m_id;
        private Int32 m_Year;
        private DateTime m_LastVisit;
        private string m_number;
        private DateTime m_AcquisitionDate;
        private EXMissionStructures_PoleTreatment m_Treatment;
        private EXMissionStructures_PoleClass m_Class;
        private EXMissionStructures_TopType m_TopType;
        private EXMissionStructures_PoleSpecies m_Specie;
        private Int32 m_Length;
        private ECompany m_Company;
        private EInspector m_Inspector;
        private EMap m_Map;
        private EVendor_Contact m_Contact;
        private EVendor_Project m_Project;
        protected DatabaseTypes m_DatabaseType;
        private EInspectionStructure m_Structure;
        private EInspectionResult m_Result;
        private bool m_isElectricalProblem;
        private ESection m_Section;
        private List<EInspectionImage> m_Images;
        private List<ERemedy> m_CrossRemedies;
        private EUtility_Asset_XMissionStructure m_UtilityAsset;
        private int m_GuardsQuantity;
        private bool m_PoleTag;
        private bool m_Billable;
        private bool m_GroundWireRepair;
        private string m_LastCompanyName;
        private EVendor_Visit_Type m_VisitType;

        #endregion

        #region Properties

        public int Id
        {
            get { return m_id; }
            set { m_id = value; }
        }

        public int Year
        {
            get { return m_Year; }
            set { m_Year = value; }
        }

        public DateTime LastVisit
        {
            get { return m_LastVisit; }
            set { m_LastVisit = value; }
        }

        public string Number
        {
            get { return m_number; }
            set { m_number = value; }
        }

        public DateTime AcquisitionDate
        {
            get { return m_AcquisitionDate; }
            set { m_AcquisitionDate = value; }
        }

        public EXMissionStructures_PoleTreatment Treatment
        {
            get { return m_Treatment; }
            set { m_Treatment = value; }
        }

        public EXMissionStructures_PoleClass Class
        {
            get { return m_Class; }
            set { m_Class = value; }
        }

        public EXMissionStructures_TopType TopType
        {
            get { return m_TopType; }
            set { m_TopType = value; }
        }

        public EXMissionStructures_PoleSpecies Specie
        {
            get { return m_Specie; }
            set { m_Specie = value; }
        }

        public int Length
        {
            get { return m_Length; }
            set { m_Length = value; }
        }

        public ECompany Company
        {
            get { return m_Company; }
            set { m_Company = value; }
        }

        public EInspector Inspector
        {
            get { return m_Inspector; }
            set { m_Inspector = value; }
        }

        public EMap Map
        {
            get { return m_Map; }
            set { m_Map = value; }
        }

        public DatabaseTypes DatabaseType
        {
            get { return m_DatabaseType; }
            set { m_DatabaseType = value; }
        }

        public List<EInspectionImage> Images
        {
            get { return m_Images; }
            set { m_Images = value; }
        }

        public EVendor_Project Project
        {
            get { return m_Project; }
            set { m_Project = value; }
        }

        public EVendor_Contact Contact
        {
            get { return m_Contact; }
            set { m_Contact = value; }
        }

        public EInspectionStructure Structure
        {
            get { return m_Structure; }
            set { m_Structure = value; }
        }

        public EInspectionResult Result
        {
            get { return m_Result; }
            set { m_Result = value; }
        }

        public ESection Section
        {
            get { return m_Section; }
            set { m_Section = value; }
        }

        public List<ERemedy> CrossRemedies
        {
            get { return m_CrossRemedies; }
            set { m_CrossRemedies = value; }
        }

        public EUtility_Asset_XMissionStructure UtilityAsset
        {
            get { return m_UtilityAsset; }
            set { m_UtilityAsset = value; }
        }

        public int GuardsQuantity
        {
            get
            {
                return m_GuardsQuantity;
            }
            set
            {
                m_GuardsQuantity = value;
            }
        }

        public bool PoleTag
        {
            get
            {
                return m_PoleTag;
            }
            set
            {
                m_PoleTag = value;
            }
        }

        public bool Billable
        {
            get
            {
                return m_Billable;
            }
            set
            {
                m_Billable = value;
            }
        }


        public bool GroundWireRepair
        {
            get
            {
                return m_GroundWireRepair;
            }
            set
            {
                m_GroundWireRepair = value;
            }
        }

        public string LastCompanyName
        {
            get
            {
                return m_LastCompanyName;
            }
            set
            {
                m_LastCompanyName = value;
            }
        }

        public EVendor_Visit_Type VendorVisitType
        {
            get
            {
                return m_VisitType;
            }
            set
            {
                m_VisitType = value;
            }
        }

        #endregion

        public EInspection()
        {
            m_AcquisitionDate = DateTime.MinValue;
            m_Class = new EXMissionStructures_PoleClass();
            m_Company = new ECompany();
            m_id = Int32.MinValue;
            m_Inspector = new EInspector();
            m_LastVisit = DateTime.MinValue;
            m_Length = Int32.MinValue;
            m_Map = new EMap();
            m_number = "";
            m_TopType = new EXMissionStructures_TopType();
            m_Treatment = new EXMissionStructures_PoleTreatment();
            m_Specie = new EXMissionStructures_PoleSpecies();
            m_Year = Int32.MinValue;
            m_Images = new List<EInspectionImage>();
            m_Project = new EVendor_Project();
            m_Contact = new EVendor_Contact();
            m_Structure = new EInspectionStructure();
            m_Result = new EInspectionResult();
            m_Section = new ESection();
            m_CrossRemedies = new List<ERemedy>();
            m_UtilityAsset = new EUtility_Asset_XMissionStructure();
        }

        public override bool Equals(object obj)
        {
            if(obj is EInspection)
            {
                EInspection tmpObj = (EInspection) obj;
                return (tmpObj.Id == this.Id);
            }else
            {
                return false;
            }
        }

        public override string GetHtmlPlacemark()
        {
            StringBuilder _Shtml = new StringBuilder();
            _Shtml.AppendLine("<table style='font-family:Verdana; font-size:10px;' border='0'>");
            //Header
            _Shtml.AppendLine(" <tr>");
            _Shtml.AppendLine("     <td colspan='2'><b>Inspection Result | Inspection [" + Id + "]</b></td>");
            _Shtml.AppendLine(" </tr>");
            _Shtml.AppendLine(" <tr>");
            _Shtml.AppendLine("     <td>");
            _Shtml.AppendLine(GenerateHtmlImages());
            _Shtml.AppendLine("     </td>");
            _Shtml.AppendLine("     <td>");
            _Shtml.AppendLine("         <table>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Result Type: " + Result.Resultype.Name + "</td>");
            _Shtml.AppendLine("                 <td>Inspection Type: " + Result.InspectionType.Name + "</td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td><b>Notes<b></td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td><ul>");
            foreach (ENote note in Result.Notes)
            {
                _Shtml.AppendLine("<li>" + note.Name + "</li>");
            }
            _Shtml.AppendLine("                 </ul></td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td colspan='2'>Comments : " + Result.Comments + "</td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Utility:</td>");
            _Shtml.AppendLine("                 <td>" + Company.Name + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Company:</td>");
            _Shtml.AppendLine("                 <td>" + Project.Vendor.Name + "</td>");
            _Shtml.AppendLine("             </tr>");


            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Inspector:</td>");
            _Shtml.AppendLine("                 <td>" + Inspector.Name + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Length:</td>");
            if (Length != Int32.MinValue)
                _Shtml.AppendLine("                 <td>" + Length + "</td>");
            else
                _Shtml.AppendLine("                 <td></td>");
            _Shtml.AppendLine("             </tr>");


            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Specie:</td>");
            _Shtml.AppendLine("                 <td>" + Specie.Name + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Top:</td>");
            _Shtml.AppendLine("                 <td>" + TopType.Name + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Class:</td>");
            _Shtml.AppendLine("                 <td>" + Class.Name + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Treatment:</td>");
            _Shtml.AppendLine("                 <td>" + Treatment.Name + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Acquisition Date:</td>");
            _Shtml.AppendLine("                 <td>" + AcquisitionDate.ToString() + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Number</td>");
            _Shtml.AppendLine("                 <td>" + Number + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Last Inspector:</td>");
            _Shtml.AppendLine("                 <td>" + LastCompanyName + "</td>");
            _Shtml.AppendLine("             </tr>");

            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Last Visit:</td>");
            _Shtml.AppendLine("                 <td>" + LastVisit + "</td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Year:</td>");
            if(Year != Int32.MinValue)
                _Shtml.AppendLine("                 <td>" + Year + "</td>");
            else
                _Shtml.AppendLine("                 <td>-</td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Map:</td>");
            _Shtml.AppendLine("                 <td>" + Map.Name + "</td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Section:</td>");
            _Shtml.AppendLine("                 <td>" + Section.Name + "</td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("             <tr>");
            _Shtml.AppendLine("                 <td>Guy Guards:</td>");
            _Shtml.AppendLine("                 <td>" + GuardsQuantity + "</td>");
            _Shtml.AppendLine("             </tr>");
            _Shtml.AppendLine("         </table>");
            _Shtml.AppendLine("     </td>");
            _Shtml.AppendLine(" </tr>");
            _Shtml.AppendLine("</table>");
            return _Shtml.ToString();
        }

        private string GenerateHtmlImages()
        {
            StringBuilder _temp = new StringBuilder();
            _temp.AppendLine("         <table border='0'>");
            foreach (EInspectionImage i in Images)
            {
                _temp.AppendLine("         <tr>");
                _temp.AppendLine(i.GenerateHTML());
                _temp.AppendLine("         </tr>");
            }
            _temp.AppendLine("         </table>");
            return _temp.ToString();
        }

        protected override string GetPositionKML()
        {
            StringBuilder s = new StringBuilder();
            s.AppendLine("<Point>");
            s.AppendLine("  <coordinates>" + Longitude.ToString().Replace(",", ".") + "," + Latitude.ToString().Replace(",", ".") + " ,10000</coordinates>");
            s.AppendLine("</Point>");
            return s.ToString();
        }

        public override string GetHtmlName()
        {
            return "Inspection " + Id.ToString();
        }

        public override string GetIcon()
        {
            string returnIcon = "";
            if (Result.IsElectrical)
            {
                returnIcon = PPI.Constants.K_INSPECTIONELECPROBICON;
            }
            else
            {
                Int32 value = EResultType.ParseValue(Result.Resultype.Name);
                switch (value)
                {
                    case 1:
                        returnIcon = PPI.Constants.K_INSPECTIONRESULT1;
                        break;
                    case 2:
                        returnIcon = PPI.Constants.K_INSPECTIONRESULT2;
                        break;
                    case 30:
                    case 31:
                    case 32:
                    case 33:
                        returnIcon = PPI.Constants.K_INSPECTIONRESULT3;
                        break;
                }
            }
            return returnIcon;
        }

        public override string GetPlacemarkId()
        {
            return "Inspection_" + Id.ToString();
        }
        
    }
}
